package com.cip.master.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.velocity.app.VelocityEngine;

import com.cip.common.util.CommonUtil;
import com.cip.common.util.GridUtil;
import com.cip.master.model.HomeWork;
import com.cip.master.service.HomeWorkService;
import com.cip.master.dao.HomeWorkDAO;

public class HomeWorkServiceImpl implements HomeWorkService  {

	private HomeWorkDAO homeWorkDAO;
	private VelocityEngine velocityEngine;
	
	public HomeWork getHomeWorkById(int id) throws Exception{
		return homeWorkDAO.getHomeWorkById(id);
	}
	
	public HomeWork saveOrUpdateHomeWork(HomeWork homeWork) throws Exception{
		return homeWorkDAO.saveOrUpdateHomeWork(homeWork);
	}
	
	public HomeWork editHomeWork(HomeWork homeWork) throws Exception{
		return homeWorkDAO.editHomeWork(homeWork);
		
	}

	public List<HomeWork> getAllHomeWorkList() throws Exception {
		return homeWorkDAO.getAllHomeWorkList();
	}
	
	public String getHomeWorkGridList(Locale locale)throws Exception {
		
		String gridSource = ""; 
		
		try {
			List<HomeWork> homeWorkList = getAllHomeWorkList();
			
			List<Map<String,Object>> homeWorkArrayList = new ArrayList<Map<String,Object>>();
			
			for (HomeWork homeWork : homeWorkList) {
				Map<String,Object> homeWorkObj = new HashMap<String, Object>();
					
				homeWorkObj.put("id", "<a href='/editHomework?method=showEditHomeWork&homeWork.id="+homeWork.getId()+"' style='text-decoration:underline;color:blue;'>"+homeWork.getId()+"</a>");
				homeWorkObj.put("classYear", homeWork.getClassYear().getName());
				homeWorkObj.put("sectionName", homeWork.getSection().getSectionName());
				homeWorkObj.put("subjectName", homeWork.getSubject().getSubjectName());
				homeWorkObj.put("description", homeWork.getDescription());
				homeWorkObj.put("date", homeWork.getDate());
				homeWorkObj.put("status", homeWork.getStatus());
				
				homeWorkArrayList.add(homeWorkObj);			
			}
			
			gridSource = generateScriptForProcess(velocityEngine, homeWorkArrayList, locale);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return gridSource;
	}
	
	/**
	 * Set into context of grid column names and field names and it attributes
	 * 
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static String generateScriptForProcess(VelocityEngine velocityEngine, List<Map<String, Object>> homeWorkArrayList, Locale locale) throws Exception {
		
		return GridUtil.generateScriptForProcess(velocityEngine, homeWorkArrayList, createHomeWorkGridColumnMapping(locale));
	}
	
	/**
	 * Add needed column and it mapping for my instances
	 * 
	 * @param context
	 */
	private static Map<String, Object> createHomeWorkGridColumnMapping(Locale locale) {
		Map<String, Object> context = new HashMap<String, Object>();
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		//ResourceBundle appResourceBundle = ResourceBundle.getBundle(Constants.BUNDLE_KEY ,locale);
		//context.put("title", appResourceBundle.getString("process.myInstances"));
		context.put("title", "HomeWork List");
		context.put("gridId", "MY_HOMEWORKLIST");
		context.put("needTreeStructure", true);
		//context.put("dynamicGridWidth", "300px");
		//context.put("dynamicGridHeight", "580px");
		String columnNames = "['Id','Class','Section','Subject','Home Work','Date','Status']";
        CommonUtil.createFieldNameList(fieldNameList, "id", "100", "center", "","false");
        CommonUtil.createFieldNameList(fieldNameList, "classYear", "100", "left","", "false");
        CommonUtil.createFieldNameList(fieldNameList, "sectionName", "100", "left","", "false");
        CommonUtil.createFieldNameList(fieldNameList, "subjectName", "100", "left","", "false");
    	CommonUtil.createFieldNameList(fieldNameList, "description", "100", "center", "", "false");
    	CommonUtil.createFieldNameList(fieldNameList, "date", "100", "center", "", "false");
    	CommonUtil.createFieldNameList(fieldNameList, "status", "100", "center", "", "false");

        context.put("columnNames", columnNames);
        context.put("noOfRecords", "10");
		context.put("fieldNameList", fieldNameList);
		context.put("gridHeading", "true");
		
		return context;
	}
	
	public List<HomeWork> getAllHomeWorkListByClass(int section ,int classYear)throws Exception {
		return homeWorkDAO.getAllHomeWorkListByClass(section,classYear);
	}
	/**
	 * @return the homeWorkDAO
	 */
	public HomeWorkDAO getHomeWorkDAO() {
		return homeWorkDAO;
	}

	/**
	 * @param homeWorkDAO the homeWorkDAO to set
	 */
	public void setHomeWorkDAO(HomeWorkDAO homeWorkDAO) {
		this.homeWorkDAO = homeWorkDAO;
	}

	public VelocityEngine getVelocityEngine() {
		return velocityEngine;
	}

	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}

}
